package com.appcenter.monitor.service;

import com.appcenter.jz.config.JsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.monitor.config.RemoteProperties;
import com.appcenter.monitor.dto.Apps;
import com.appcenter.monitor.expand.DicArray;
import com.appcenter.monitor.input.logs.GetLogsInput;
import com.appcenter.monitor.input.logs.PageLoadInput;
import com.appcenter.monitor.mapper.LogsMapper;
import com.appcenter.monitor.model.Logs;
import com.appcenter.monitor.output.logs.GetLogsOutput;
import com.appcenter.monitor.output.logs.PageLoadOutput;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.appcenter.jz.unit.ObjectUnit.JsonUnit;
import static org.apache.http.util.TextUtils.isBlank;

@Service
public class LogsService {


    @Autowired
    LogsMapper logsMapper;


    @Autowired
    RemoteProperties remoteProperties;

    @Autowired
    HttpAPIService httpAPIService;


    @Autowired
    ObjectMapper objectMapper;


    /**
     *  创建人:Coke
     *  备注: 获取系统日志
     *  时间: 2019/4/26 14:52 
     *  参数: 
     */
    public GetLogsOutput GetLogs(GetLogsInput input) {
        GetLogsOutput output = new GetLogsOutput();
        Example example = new Example(Logs.class);
        Example.Criteria criteria = example.createCriteria();
        if(!isBlank(input.getKeyword())){
            criteria.andLike("message","%"+input.getKeyword()+"%");
        }
        if(!isBlank(input.getAppCode())){
            criteria.andEqualTo("appCode",input.getAppCode());
        }
        if(!isBlank(input.getLogType())){
            criteria.andEqualTo("logType",input.getLogType());
        }
        if(!isBlank(input.getLevel())){
            criteria.andEqualTo("level",input.getLevel());
        }
        criteria.andEqualTo("appCode", input.getAppCode());
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"id desc");
        List<Logs> ls = logsMapper.selectByExample(example);
        PageInfo<Logs> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Logs>(page.getList(), (int)page.getTotal()));
        return output;
    }


    /**
     *  创建人:Coke
     *  备注: 加载系统日志
     *  时间: 2019/4/26 14:53 
     *  参数: 
     */
    public PageLoadOutput PageLoad(PageLoadInput input) throws IOException {
        PageLoadOutput output = new PageLoadOutput();

        List<DicArray> listlogTypes=new ArrayList<DicArray>();
        DicArray logTypes=new DicArray();
        logTypes.setKey("Business");
        logTypes.setText("业务");
        listlogTypes.add(logTypes);

        logTypes=new DicArray();
        logTypes.setKey("System");
        logTypes.setText("系统");
        listlogTypes.add(logTypes);

        List<DicArray> listlevels=new ArrayList<DicArray>();
        DicArray levels=new DicArray();
        levels.setKey("Debug");
        levels.setText("调试");
        listlevels.add(levels);

        levels=new DicArray();
        levels.setKey("Error");
        levels.setText("错误");
        listlevels.add(levels);

        levels=new DicArray();
        levels.setKey("Fatal");
        levels.setText("致命");
        listlevels.add(levels);

        levels=new DicArray();
        levels.setKey("Information");
        levels.setText("信息");
        listlevels.add(levels);

        levels=new DicArray();
        levels.setKey("Warning");
        levels.setText("警告");
        listlevels.add(levels);

        output.setLevels(listlevels);
        output.setLogTypes(listlogTypes);



        String GetAppsUrl = remoteProperties.getAppCenterServiceUrl() + "/app/getapps";
        String responseJson = httpAPIService.JsonSMS(GetAppsUrl, "{}");
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        JsonResult jsonResult = objectMapper.readValue(responseJson, JsonResult.class);
        List<Apps> apps = new ArrayList<Apps>();
        Object OneItems = JsonUnit(jsonResult.getResult(), "items");
        ArrayList<Object> TwoItems = (ArrayList<Object>) JsonUnit(OneItems, "items");
        if (TwoItems != null && TwoItems.size() > 0) {
            for (int i = 0; i < TwoItems.size(); i++) {
                Apps app = new Apps();
                app.setId(JsonUnit(TwoItems.get(i), "id").toString());
                app.setAppCode(JsonUnit(TwoItems.get(i), "appCode").toString());
                app.setAppName(JsonUnit(TwoItems.get(i), "appName").toString());
                apps.add(app);
            }
        }
        output.setApps(apps);
        output.setItems(GetLogs(input).getItems());
        return output;
    }
}
